﻿using B.MESUI.Login.EquipmentManagement.DTO;
using B.MESUI.Login.EquipmentManagement.Model;
using DevExpress.ClipboardSource.SpreadsheetML;
using DevExpress.XtraEditors;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static B.MESUI.Login.WareHouseDefinitionForm;

namespace B.MESUI.Login.EquipmentManagement.ToolingFixturesService
{
    public partial class UDToolingFixtures : DevExpress.XtraEditors.XtraForm
    {
        private readonly int i;
        private readonly ToolingFixtures data;
        public UDToolingFixtures()
        {
            InitializeComponent();
            this.CenterToParent();
            this.Text="添加工装夹具";
        }
        public UDToolingFixtures(ToolingFixtures tooling)
        {
            InitializeComponent();
            this.CenterToParent();
            data=tooling;
            i = 1;
            this.Text = "修改工装夹具";
            this.textEdit1.Text = tooling.SerialNumber;
            this.comboBoxEdit1.EditValue = tooling.TypeId;
            this.textEdit2.Text = tooling.Size;
            this.textEdit3.Text= tooling.Model;
            this.textEdit4.Text= tooling.Brand;
            this.textEdit5.Text= tooling.Num.ToString();
            this.textEdit6.Text= tooling.ServiceLife.ToString();
            this.comboBoxEdit2.EditValue= tooling.UnitId;
            this.comboBoxEdit3.EditValue= tooling.SupplierId;
            this.richTextBox1.Text= tooling.Remark;
        }

        private void simpleButton2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private async void simpleButton1_Click(object sender, EventArgs e)
        {
            var tooling = new ToolingFixtures
            {
                Id="1",
                CreatedTime=DateTime.Now,
                CreatedUserName="admin",
                SerialNumber = this.textEdit1.Text,
                TypeId = this.comboBoxEdit1.EditValue?.ToString(),
                Size = this.textEdit2.Text,
                Model = this.textEdit3.Text,
                Brand = this.textEdit4.Text,
                Num = int.TryParse(this.textEdit5.Text, out int num) ? num : 0,
                ServiceLife = int.TryParse(this.textEdit6.Text, out int life) ? life : 0,
                UnitId = this.comboBoxEdit2.EditValue?.ToString(),
                SupplierId = this.comboBoxEdit3.EditValue?.ToString(),
                Remark = this.richTextBox1.Text
            };
            using (HttpClient client = new HttpClient())
            {
                var url = string.Empty;
                if (i== 1)
                {
                    tooling.Id=data.Id;
                    url = $"{BaseUrl.Url}ToolingFixtures/Update";
                    var jsonStr = JsonConvert.SerializeObject(tooling);
                    var content = new StringContent(jsonStr, Encoding.UTF8, "application/json");
                    var response = await client.PutAsync(url, content);
                    response.EnsureSuccessStatusCode();
                    var json = await response.Content.ReadAsStringAsync();
                    var result = JsonConvert.DeserializeObject<ApiResult<int>>(json);
                    MessageBox.Show(result.Message);
                    // 保存逻辑...
                    this.DialogResult = DialogResult.OK; // 关闭窗口并返回OK
                    this.Close();
                }
                else
                {
                    url = $"{BaseUrl.Url}ToolingFixtures/Add";
                    var jsonStr = JsonConvert.SerializeObject(tooling);
                    var content = new StringContent(jsonStr, Encoding.UTF8, "application/json");
                    var response = await client.PostAsync(url, content);
                    response.EnsureSuccessStatusCode();
                    var json = await response.Content.ReadAsStringAsync();
                    var result = JsonConvert.DeserializeObject<ApiResult<int>>(json);
                    MessageBox.Show(result.Message);
                    // 保存逻辑...
                    this.DialogResult = DialogResult.OK; // 关闭窗口并返回OK
                    this.Close();
                }
            }
        }
    }
}